package com.ilvyou.api.controller.common;

import com.ilvyou.api.bean.result.Result;
import com.ilvyou.api.common.Cache;
import com.ilvyou.api.common.Constant;
import com.ilvyou.api.controller.BaseController;
import com.ilvyou.api.service.UserService;
import com.ilvyou.core.dao.Criteria;
import com.ilvyou.core.dao.Query;
import com.ilvyou.core.util.StringUtil;
import com.ilvyou.data.lhf.ScrAlipayEntity;
import com.ilvyou.data.lhf.ScrAnswerEntity;
import com.ilvyou.data.lhf.ScrQuestionEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by GuanYuCai on 2016/9/5 0005.
 */
@Controller
public class SecurityController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/security/sendSMS", method = RequestMethod.POST)
    @ResponseBody
    public Result sendSMS(@RequestParam String mobile, @RequestParam int type) throws Exception {
        if (!StringUtil.isMobile(mobile)){
            return fail("手机号码格式不正确");
        }

        ScrAlipayEntity alipay = userService.getScrAlipayEntityByAccount(mobile);
        switch (type){
            case Constant.SMSType.REGISTER:
            case Constant.SMSType.BIND_MOBILE:
                if (alipay != null && alipay.getEnableflag() != 0){
                    return fail("该手机号已绑定乐汇付，请更换");
                }
                break;
            case Constant.SMSType.ADD_BANKNUM:
                break;
            default:
                if (alipay == null || alipay.getEnableflag() == 0){
                    return fail("该手机号不存在");
                }
                break;
        }

        long sendId = userService.sendSms(mobile, type);
        if (sendId == 0){
            return fail("发送短信失败！");
        }else{
            return success("sendid", sendId);
        }
    }

    @RequestMapping(value = "/security/verifySMS", method = RequestMethod.POST)
    @ResponseBody
    public Result verifySMS(@RequestParam Long sendid, @RequestParam String code,
                                       @RequestParam String mobile, @RequestParam int type) throws Exception {
        long time = userService.getSmsDeadline(sendid, code, mobile, type);
        if (time <= System.currentTimeMillis()){
            return fail("验证码不正确");
        }else{
            String uuid = StringUtil.UUID();
            Cache.putValidation(uuid, new Cache.Validation(mobile, time, type));
            return success("uuid", uuid);
        }
    }

    @RequestMapping("/security/questionList")
    @ResponseBody
    public Result question() throws Exception{
        return success(userService.getMapList(new Query(ScrQuestionEntity.class, "id as questionid,question")));
    }

    @RequestMapping("/security/method")
    @ResponseBody
    public Result forgetPassword(@RequestParam String account) throws Exception{
        ScrAlipayEntity scrAlipay = userService.getScrAlipayEntityByAccount(account);

        if (scrAlipay == null || scrAlipay.getEnableflag() == 0){
            return fail("该用户不存在");
        }

        Map<String, Object> map = userService.getMap(ScrAnswerEntity.class, "id as answerid,question",
                new Criteria().add("actid=?", scrAlipay.getActid()));
        map.put("actid", scrAlipay.getActid());
        map.put("mobile", scrAlipay.getMobile());
        return success(map);
    }

    @RequestMapping(value = "/security/verifyAnswer", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> questionValidation(@RequestParam Long answerid, @RequestParam String answer,
                                             @RequestParam int type, @RequestParam String mobile) throws Exception{
        ScrAlipayEntity alipay = userService.getScrAlipayEntityByAccount(mobile);
        if (alipay == null){
            return fail("无此用户");
        }

        ScrAnswerEntity a = userService.getByPK(ScrAnswerEntity.class, answerid);
        if (a == null || !answer.equals(a.getAnswer())){
            return fail("答案错误");
        }

        String uuid = StringUtil.UUID();
        Cache.putValidation(uuid, new Cache.Validation(mobile, System.currentTimeMillis() + 15 * 60 * 1000, type));
        return success("uuid", uuid);
    }
}
